package sort; /**
 * @author spf
 * @date 2024/5/1
 * @time 21:20
 */

import java.util.Arrays;

/**
 * 冒泡排序
 */
public class BubboSort {

    public static void main(String[] args) {
        int[] values = {1, 21, 12, 35, 45, 20, 2, 6, 7, 9};  // 静态初始化
        System.out.println(Arrays.toString(values));
        while (true) {
            bubboSort(values);
        }

    }

    private static void bubboSort(int[] values) {
        int temp;
        for (int i = 0; i < values.length; i++) {
            boolean flag = true;
            for (int j = 0; j < values.length - 1 - i; j++) {
                if (values[j] > values[j + 1]) {
                    temp = values[j];
                    values[j] = values[j + 1];
                    values[j + 1] = temp;

                    flag = false;
                }
            }

            if (flag) {
                break;
            }

            System.out.println((i + 1) + "次迭代：" + Arrays.toString(values));
        }

    }
}
